package com.guofen.service.earthquake.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.EarthquakeMapper;
import com.guofen.pojo.Earthquake;
import com.guofen.service.earthquake.EarthquakeService;
import com.guofen.util.uta.EarthquakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class EarthquakeServiceImpl implements EarthquakeService {

    @Autowired
    private EarthquakeMapper mapper;

    @Override
    public Map<String, Object> getAllEarthquake(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectAll();
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByDistance(double lon, double lat, double distance,
                                             int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByDistance(lat, lon, distance);
        PageInfo<Earthquake> pageUser = new PageInfo<>(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByDistanceAndMag(double lon, double lat, double distance,
                                                   double mag1, double mag2,
                                                   int pageSize, int currentPage) {
        if (mag1 > mag2) {
            double temp = mag1;
            mag1 = mag2;
            mag2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByDistanceAndMag(lat, lon, distance, mag1, mag2);
        PageInfo<Earthquake> pageUser = new PageInfo<>(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByMag(double mag1, double mag2, int pageSize, int currentPage) {
        if (mag1 > mag2) {
            double temp = mag1;
            mag1 = mag2;
            mag2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByMag(mag1, mag2);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public List<Integer> getMagDistribution(int limit) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            list.add(mapper.selectMagCount((double)i, (double)i+1, limit));
        }
        return list;
    }

    @Override
    public List<Integer> getDepthDistribution(int limit) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            list.add(mapper.selectDepthCount((double)i*100,
                                            (double)(i+1)*100, limit));
        }
        return list;
    }

    @Override
    public Map<String, Object> getByDate(long startDate, long endDate, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String start = dateFormat.format(new Date(startDate));
        String end = dateFormat.format(new Date(endDate));
        List<Earthquake> list = mapper.selectByDate(start, end);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(String startTime, String endTime, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByTime(startTime, endTime);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByDateAndMag(long startDate, long endDate,
                                               double mag1, double mag2,
                                               int pageSize, int currentPage) {
        if (mag1 > mag2) {
            double temp = mag1;
            mag1 = mag2;
            mag2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String start = dateFormat.format(new Date(startDate));
        String end = dateFormat.format(new Date(endDate));
        List<Earthquake> list = mapper.selectByDateAndMag(mag1, mag2, start, end);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTimeAndMag(String startTime, String endTime,
                                               double mag1, double mag2,
                                               int pageSize, int currentPage) {
        if (mag1 > mag2) {
            double temp = mag1;
            mag1 = mag2;
            mag2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByTimeAndMag(mag1, mag2, startTime, endTime);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByPlace(String place, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Earthquake> list = mapper.selectByPlace(place);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Earthquake getById(String id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int delete(String id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Earthquake earthquake) {
        return mapper.updateByPrimaryKeySelective(earthquake);
    }

    @Override
    public int insert(Earthquake earthquake) {
        return mapper.insertSelective(earthquake);
    }
}
